// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import StartUp from "../../../lib/StartUp";
import CM from "../../../lib/cache/CM";
import { monitor, selfCls } from "../../../lib/common/Decorator";
import { Manager } from "../../../lib/common/Manager";
import ResMgr from "../../../lib/common/ResMgr";
import DBattle from "../../cache/DBattle";
import DField from "../../cache/DField";
import DBLevel from "../../config/DBLevel";
import DBMap from "../../config/DBMap";

export interface MapData {
    width:number;
    height:number;
    content:Array<number>;
}

const {ccclass, property} = cc._decorator;

@selfCls
export default class FieldManager extends Manager {
    get parentRoot(): cc.Node {
        return StartUp.instance.gameRoot;
    }

    public do(): Promise<void> {
        return new Promise<void>((resolve, reject)=>{
            //resolve();
            let level = CM.ins(CM).get(DBattle).level;
            let mapId = DBLevel.getMapId(level);
            let mapRes = DBMap.getMapRes(mapId);
            ResMgr.loadRes(mapRes, (err, res)=>{
                let data:MapData = res.json;
                CM.ins(CM).get(DField).col = data.width;
                CM.ins(CM).get(DField).row = data.height;
                CM.ins(CM).get(DField).content = data.content;
                CM.ins(CM).get(DField).width = data.width*DBMap.getMapSize();
                CM.ins(CM).get(DField).height = data.height*DBMap.getMapSize();
                resolve();
            })
        })
    }


}
